home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / snpd9611.zip / FSIF.C < prev    next >
C/C++ Source or Header  |  1996-11-24  |  6KB  |  157 lines

  1. /* +++Date last modified: 12-Nov-1996 */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <io.h>
  7. #include "bacstd.h"
  8.  
  9. MODULEINF("1992-08-01", "1990-1992 Erik Bachmann (E-mail: ebp@dde.dk" ) ;
  10.  
  11. #define  BUFFERSIZE  512
  12.  
  13. /*
  14.  /=======================================\
  15. |     FIND_STR_IN_FILE                    |-----------------------------------|
  16. |\=======================================/
  17. |
  18. | Searches a binary file for a string.
  19. |
  20. | Returns the start offset for the first occurrence of the string.
  21. |
  22. |
  23. |
  24. |
  25. |-----------------------------------------------------------------------------|
  26. | CALL:
  27. |    find_str_in_file(Filename, startoffset, string);
  28. |
  29. | HEADERS:
  30. |    stdio.h
  31. |    string.h
  32. |    malloc.h
  33. |    io.h
  34. |
  35. | GLOBALE VARIABLES:
  36. |    %
  37. |
  38. | ARGUMENTS:
  39. |    fpFile      : Pointer to the (open) binary file
  40. |    lOffset     : Startoffset for search
  41. |    pszStr      : String to search for
  42. |
  43. | PROTOTYPE:
  44. |    long _CfnTYPE find_str_in_file(FILE *fpFile, long lOffset,
  45. |                                   char *pszStr);
  46. |
  47. | RETURN VALUE:
  48. |    long lStatusFlag   : -1   : String not found
  49. |                         -2   : Not enough memory to perform search
  50. |                         else : Offset for string in file
  51. |
  52. | MODULE:
  53. |    fsif.c
  54. |-----------------------------------------------------------------------------|
  55. |1994-03-18/Bac
  56. |   -  Enhanced error detection. Returning error value
  57. |1995-06-19/Bac
  58. |   -  Patch for returning position. Changed from blockwise to bytewise
  59. |      calculation.
  60. |1995-09-20/Bac
  61. |  -  Ajusted secondary search in last block
  62. |
  63. |1995-10-27/Bac
  64. |  -  Released to public domain
  65. |
  66. |-----------------------------------------------------------------------------|
  67. |1992-08-01/Erik Bachmann
  68. \===========================================================================|*/
  69.  
  70. long _CfnTYPE find_str_in_file(FILE *fpFile, long lOffset, char *pszStr)
  71. {
  72.       long  lStatusFlag = -1l,            /* Status: -1 = not found     */
  73.             lPosInFile  = 0l,             /* Position in file           */
  74.             lCurrentPos = 0l;
  75.  
  76.       char  cFound      = FALSE,          /* Flag: FALSE = Not found (yet) */
  77.             *pszBuffer  = NULL;           /* Buffer for fileinput       */
  78.  
  79.       int   iDataSize   = 0,              /* Size of data read from file*/
  80.             iSector     = 0,              /* No of blocks read          */
  81.             offset      = 0,              /* local counter              */
  82.             iBufferSize = BUFFERSIZE,
  83.             iNoBlocks   = 0;              /* No of blocks remaining     */
  84.  
  85.       /*----------------------------------------------------------------*/
  86.  
  87.       lPosInFile = filelength(fileno(fpFile));  /* Find filesize        */
  88.  
  89.       iNoBlocks = (int)(lPosInFile / (int) iBufferSize) ;
  90.                                     /* Calculate remaining no of blocks */
  91.  
  92.       fseek(fpFile, lOffset, SEEK_SET) ;  /* Go to start offset */
  93.  
  94.       lCurrentPos = lOffset ;
  95.  
  96.       iNoBlocks = (int)((lPosInFile - lOffset) / (int) iBufferSize);
  97.                                     /* Calculate remaining no of blocks */
  98.  
  99.       if (0 == (pszBuffer = (char *) calloc(2 * iBufferSize, sizeof(char))))
  100.             return -2;              /* Allocate buffer                  */
  101.  
  102.       memset(pszBuffer, '\0', iBufferSize);
  103.  
  104.       iDataSize = fread(pszBuffer, sizeof(char), iBufferSize, fpFile);
  105.                                     /* Read the first block             */
  106.  
  107.       while ((0 < iNoBlocks) && !cFound)
  108.       {                             /* Repeat until EOF or found        */
  109.             iSector++;              /* Counting no of blocks read       */
  110.             iNoBlocks--;
  111.  
  112.             memset(&pszBuffer[iBufferSize], '\0', iBufferSize);
  113.             iDataSize = fread(&pszBuffer[iBufferSize], sizeof(char),
  114.                               iBufferSize, fpFile); /* Read next block  */
  115.  
  116.             for (offset = 0; offset < iBufferSize; offset++)
  117.                                     /* Search first block               */
  118.             {
  119.                   if (0 == strncmp(&pszBuffer[ offset ],
  120.                                    pszStr, strlen(pszStr)))
  121.                   {                 /* Is the string placed here?       */
  122.                         cFound = TRUE;    /* Yes -> set flag            */
  123.                         break;
  124.                   }
  125.                   else  lCurrentPos++;    /* No -> Try again            */
  126.             }
  127.  
  128.             memcpy(pszBuffer, &pszBuffer[ iBufferSize ], iBufferSize);
  129.                                     /* Shift block left                 */
  130.       }
  131.  
  132.       /* Search the last Sector read if tag not found yet               */
  133.  
  134.       if (!cFound)
  135.       {
  136.             iSector++;              /* Counting no of blocks read       */
  137.  
  138.             for (offset = 0; offset < iDataSize - strlen(pszStr); offset++)
  139.                   if (0 == strncmp(&pszBuffer[ offset ], pszStr,
  140.                                    strlen(pszStr)))
  141.                   {                 /* Is the string placed in last block?*/
  142.                         cFound = TRUE;    /* Found -> set flag          */
  143.                         break;
  144.                   }
  145.                   else if (lCurrentPos < lPosInFile)
  146.                                     /* Check for End of File            */
  147.                         lCurrentPos++;    /* In file -> goto next       */
  148.                   else  break;            /* End of File = quit         */
  149.       }
  150.  
  151.       free(pszBuffer);              /* Free the allocated memory again  */
  152.  
  153.       if (TRUE == cFound)                 /* IF tag is found            */
  154.             return ((long) lCurrentPos);  /* Return bytewise positon    */
  155.       else  return lStatusFlag;           /* Return errorcode           */
  156. }
  157.